Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
emitter-component
Advanced tools
The emitter-component npm package is a simple event emitter implementation. It allows you to create objects that can emit events and have listeners that respond to those events. This is useful for decoupling different parts of an application and enabling communication between them.
Event Emission
This feature allows you to emit events and have listeners respond to those events. In the code sample, an event listener is defined for the 'event' event, and then the event is emitted with a message.
const Emitter = require('emitter-component');
const emitter = new Emitter();
// Define an event listener
emitter.on('event', function(message) {
console.log('Event received:', message);
});
// Emit an event
emitter.emit('event', 'Hello, World!');
Removing Event Listeners
This feature allows you to remove event listeners. In the code sample, an event listener is defined and then removed before the event is emitted, so no listeners respond to the event.
const Emitter = require('emitter-component');
const emitter = new Emitter();
function onEvent(message) {
console.log('Event received:', message);
}
// Define an event listener
emitter.on('event', onEvent);
// Remove the event listener
emitter.off('event', onEvent);
// Emit an event (no listeners should respond)
emitter.emit('event', 'Hello, World!');
Once Event Listeners
This feature allows you to define event listeners that will be called only once. In the code sample, the event listener is defined with the 'once' method and is called only the first time the event is emitted.
const Emitter = require('emitter-component');
const emitter = new Emitter();
// Define an event listener that will be called only once
emitter.once('event', function(message) {
console.log('Event received:', message);
});
// Emit the event twice
emitter.emit('event', 'Hello, World!');
emitter.emit('event', 'Hello again!');
The 'events' package is the built-in Node.js event emitter module. It provides a similar API to emitter-component but is more feature-rich and is maintained as part of the Node.js core. It is widely used and well-documented.
The 'eventemitter3' package is a high-performance event emitter for Node.js and the browser. It is similar to emitter-component but offers better performance and additional features like wildcard event listeners.
The 'mitt' package is a tiny (~200 bytes) functional event emitter. It is similar to emitter-component but is designed to be extremely lightweight and simple, making it suitable for use in performance-critical applications.
Event emitter component.
$ component install component/emitter
The Emitter
may also be used as a mixin. For example
a "plain" object may become an emitter, or you may
extend an existing prototype.
As an Emitter
instance:
var Emitter = require('emitter');
var emitter = new Emitter;
emitter.emit('something');
As a mixin:
var Emitter = require('emitter');
var user = { name: 'tobi' };
Emitter(user);
user.emit('im a user');
As a prototype mixin:
var Emitter = require('emitter');
Emitter(User.prototype);
Register an event
handler fn
.
Register a single-shot event
handler fn
,
removed immediately after it is invoked the
first time.
event
and fn
to remove a listener.event
to remove all listeners on that event.Emit an event
with variable option args.
Return an array of callbacks, or an empty array.
Check if this emitter has event
handlers.
FAQs
Event emitter
The npm package emitter-component receives a total of 600,081 weekly downloads. As such, emitter-component popularity was classified as popular.
We found that emitter-component demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 22 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.